package com.rgst.controller;

import java.util.List;
import java.util.stream.Collectors;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.rgst.anno.DontLogin;
import com.rgst.dao.entity.Busi;
import com.rgst.dao.repository.BusiRepository;

/**
 * Created by tancw on 2019/11/19.
 * https://www.cnblogs.com/java-hui/p/11228601.html
 */

@Controller
public class AdminController extends BaseController {

    @Resource
    private BusiRepository busiRepository;

    @RequestMapping("to")
    @DontLogin
    public String toPage(String page) {
        return page;
    }

    @GetMapping("/")
    public String index(HttpServletRequest request) {
        if (noSession(request)) {
            return "login";
        }
        return "redirect:loadMain";
    }

    @GetMapping("loadMain")
    public String load(HttpServletRequest request, Model model) {
        List<Busi> busiList = busiRepository.findAll(new Sort(Sort.Direction.ASC, "orderNo"));
       List<Busi> activeList =  busiList.stream().filter(s->s.isValid()).collect(Collectors.toList());
        model.addAttribute("activeList", CollectionUtils.isEmpty(activeList)? null: activeList);
        model.addAttribute("busiList", busiList.stream().limit(20).collect(Collectors.toList()));
        return "main";
    }
}
